/**
 * @author chen han
 */

package zheezes.util;

import java.io.File;

public class SwtJarGetter {
	private final static String[] prefixes = { 
		"org.eclipse.osgi",
		"org.eclipse.ui.forms", 
		"org.eclipse.core.commands",
		"org.eclipse.equinox.common", 
		"org.eclipse.equinox.registry",
		"org.eclipse.core.runtime", 
		"org.eclipse.text",
		"org.eclipse.swt.win32.win32.x86", 
		"org.eclipse.jface",
		"org.eclipse.jface.text", 
		"org.eclipse.ui.workbench",
		"org.eclipse.core.databinding.beans",
		"org.eclipse.core.databinding", 
		"org.eclipse.jface.databinding",
		"com.ibm.icu"	
	};

	private File findJar(File dir, String prefix) {
		for (File file : dir.listFiles()) {
			if (file.getName().startsWith(prefix + "_")) {
				return file;
			}
		}
		return null;
	}

	private File findSrc(File dir, String prefix) {
		for (File file : dir.listFiles()) {
			if (file.getName().startsWith(prefix + ".source_")) {
				return file;
			}
		}
		return null;
	}

	private String trimPrefix(String filename) {
		return filename.substring(0, filename.indexOf('_')) + ".jar";
	}

	public void copy() {
		File echome = new File("D:/usr/eclipse/plugins");
		File dest = new File("C:/swt");
		File file = null;
		if (dest.exists()) {
			FileUtil.delete(dest);
		}
		if (dest.mkdir()) {
			for (String prefix : prefixes) {
				if ((file = findJar(echome, prefix)) != null) {
					FileUtil.copy(new File(dest, trimPrefix(file.getName())), file);
				}
			}
		}
	}
	
	public void print() {
		File echome = new File("D:/usr/eclipse/plugins");
		File file = null;
		File src = null;
		for (String prefix : prefixes) {
			if ((file = findJar(echome, prefix)) != null) {
				if ((src = findSrc(echome, prefix)) != null) {
					System.out.println(String.format("<classpathentry kind=\"lib\" path=\"%s\" sourcepath=\"%s\"/>", file.getAbsolutePath(), src.getAbsolutePath()));
				} else {
					System.out.println(String.format("<classpathentry kind=\"lib\" path=\"%s\"/>", file.getAbsolutePath()));
				}
			}
		}
	}

	public static void main(String[] args) {
		new SwtJarGetter().copy();
	}
}
